package com.cictec.middleware.gps.engine.position.handler.alarms;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cictec.middleware.commons.utils.Constants;
import com.cictec.middleware.commons.utils.DateUtils;
import com.cictec.middleware.core.entity.BusPark;
import com.cictec.middleware.core.entity.BusParkReturnNote;
import com.cictec.middleware.core.entity.WarnMsg;
import com.cictec.middleware.gps.engine.BusInstance;
import com.cictec.middleware.gps.engine.exception.BusPositionWarnHandlerException;
import com.cictec.middleware.gps.engine.position.handler.BusPositionAlarmsHandler;
import com.cictec.middleware.gps.service.BusParkService;
import com.cictec.middleware.gps.service.WarnMsgService;

/**
 * 回场报警
 * @author ql
 * @date 2017年9月26日
 */
@Component
public class BusParkingWarnHandler implements WarnHandler {

	private static Logger logger = LoggerFactory.getLogger(BusPositionAlarmsHandler.class);
	
	@Autowired
	private WarnMsgService warnMsgService;
	@Autowired
	private BusParkService busParkService;
	
	@Override
	public String getWarnType() {
		return Constants.VAL_BUS_STATUS_9;
	}
	
	@Override
	public void onHandler(BusInstance busInstance) throws BusPositionWarnHandlerException {
		
		logger.debug("【回场报警信息处理】");
		
		BusPark park = busParkService.isInPark(busInstance.getBusUuid(), busInstance.getLatitude(), busInstance.getLongitude());
		// 根据GPS点能查出来场站，表示车在张展内
		boolean isInPark = park != null? true:false;
		
		boolean preIsInPark = busInstance.isInPark();// 前一个点的状态
		if(preIsInPark != isInPark){
			// 状态发生变化
			busReturnNoteHandler(busInstance, park, isInPark); // 记录车辆回场状态
			
			busInstance.setInPark(park != null? true:false);
		}
		
		
		boolean  isShouldPark = busParkService.isShouldInPark(busInstance.getBusUuid(), busInstance.getLineUuid());
		busInstance.setShouldPark(isShouldPark);
		
		if (isShouldPark && !isInPark) {
			// 应该在场站，但是不再场站，车辆未回场
			startWarn(busInstance);
		}else{
			stopWarn(busInstance);
		}
		
		// 回场统计
		
	}

	public void startWarn(BusInstance busInstance){
		WarnMsg warnMsg = busInstance.getWarnsByWarnType(Constants.VAL_BUS_STATUS_10);
		if(warnMsg != null){
			return;
		}
		
		String currentTime = DateUtils.formatDateTimes(new Date());
		warnMsg = new WarnMsg();
		warnMsg.setWarnBusUuid(busInstance.getBusUuid());
		warnMsg.setWarnDevUuid(busInstance.getBusUuid());
		warnMsg.setWarnDrvUuid(busInstance.getDriverUuid());
//		warnMsg.setWarnLineUuid(busInstance.getLineUuid());
		warnMsg.setWarnLineUuid(busInstance.getLineUuid());
		warnMsg.setWarnType(Constants.VAL_BUS_STATUS_10);

		warnMsg.setWarnStartLat(busInstance.getLatitude());
		warnMsg.setWarnStartLng(busInstance.getLongitude());

		warnMsg.setWarnStartTime(currentTime);
		warnMsg.setWarnSpeed(busInstance.getSpeed());
		warnMsg.setWarnStorageTime(currentTime);

		warnMsg.setWarnOperStatus(Constants.VAL_BUS_OPER_STATUS_0);
		warnMsg.setWarnClosedFlag(Constants.VAL_WARN_CLOSED_0);

		logger.debug("【车辆回场】{}回场报警开始", busInstance.getBusNumber());
		warnMsgService.saveWarn(warnMsg);
		
	}
	
	@Override
	public void stopWarn(BusInstance busInstance) {
		WarnMsg warnMsg = busInstance.getWarnsByWarnType(Constants.VAL_BUS_STATUS_10);
		if(warnMsg == null){
			return;
		}
		// 结束回场报警
		warnMsg.setWarnClosedFlag(Constants.VAL_WARN_CLOSED_1);
		warnMsg.setWarnEndTime(DateUtils.formatDateTimes(new Date()));
		warnMsg.setWarnEndLat(busInstance.getLatitude());
		warnMsg.setWarnEndLng(busInstance.getLongitude());
		logger.debug("【车辆回场】{}回场报警关闭", busInstance.getBusNumber());
		warnMsgService.saveWarn(warnMsg);
		
		busInstance.setWarns(Constants.VAL_BUS_STATUS_10, null);	// 清空实时对象报警
	}

	/**
	 * 车辆回场统计逻辑
	 * @param busInstance
	 * @param busPark
	 * @param isInPark
	 */
	private void busReturnNoteHandler(BusInstance busInstance, BusPark busPark, boolean isInPark) {

		/*
		 * 1、查询数据库车辆回场状态 2、判定回场状态是更新还是新增
		 */
		String date = DateUtils.getDate();
		String busUuid = busInstance.getBusUuid();
		String busLineUuid = busInstance.getBus().getBusLineUuid(); // 车辆所属线路


		BusParkReturnNote note = busParkService.getBusReturnParkNote(date, busUuid);

		if (note == null) {
			note = new BusParkReturnNote();

			note.setBrpDate(date);
			note.setBrpBusUuid(busUuid);
			note.setBrpDevUuid(busInstance.getDevUuid());
			note.setBrpLineUuid(busLineUuid);
			note.setBrpParkUuid(busPark == null ? null : busPark.getParkUuid()); // 不为null时候会更新场站，即进场状态会更新
		}

		if (!isInPark) {
			note.setBrpOutDrvUuid(busInstance.getDriverUuid());
			note.setBrpOutTime(DateUtils.formatDateTimes(busInstance.getTime()));
			note.setBrpStatus("0");
		} else {
			note.setBrpReturnDrvUuid(busInstance.getDriverUuid());
			note.setBrpReturnTime(DateUtils.formatDateTimes(busInstance.getTime()));
			note.setBrpStatus("1");
		}
		busParkService.saveBusParkReturnNote(note);
	}





	
	
}
